package com.schoolcloud.system.controller;

import com.schoolcloud.common.model.R;
import com.schoolcloud.system.entity.SysPermission;
import com.schoolcloud.system.service.SysPermissionService;
import com.schoolcloud.system.vo.SysPermissionTreeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("system/permission")
public class SysPermissionController {

    private final SysPermissionService permissionService;


    @GetMapping("/{permissionId}")
    public R<SysPermission> getPermissionById(@PathVariable String permissionId) {
       SysPermission permission= permissionService.getPermissionById(permissionId);
        return R.ok(permission);
    }


    @PostMapping
    public R<Object> addPermission(@RequestBody SysPermission permission) {
        permissionService.addPermission(permission);
        return R.ok();
    }

    @PutMapping
    public R<Object> modifyPermission(@RequestBody SysPermission permission) {
        permissionService.modifyPermission(permission);
        return R.ok();
    }

    @GetMapping("code/user")
    public R<List<String>> getUserPermissionCode(String userId) {
        List<String> permissionsCode = permissionService.getUserPermissionsCode(userId);
        return R.ok(permissionsCode);
    }
    @GetMapping("/by/roleId/{roleId}")
    public R<List<SysPermission>> getUserPermissionsByRoleId(@PathVariable String roleId) {
        return R.ok( permissionService.getPermissionsByRoleId(roleId));
    }

    @GetMapping("tree-list")
    public R<List<SysPermissionTreeVO>> getTreeList(SysPermission sysPermission) {
        List<SysPermission> list = permissionService.list(sysPermission);
//        转换为树结构
        List<SysPermissionTreeVO> treeList = permissionService.convertedPermissionToTreeList(list);
        return R.ok(treeList);
    }

    /**
     * 获取非按钮级的权限
     *
     * @return {@link R}<{@link List}<{@link SysPermissionTreeVO}>>
     */
    @GetMapping("/folder")
    public R<List<SysPermissionTreeVO>> getPermissionFolder() {
        List<SysPermission> list = permissionService.getPermissionFolder();
        List<SysPermissionTreeVO> treeList = permissionService.convertedPermissionToTreeList(list);
        return R.ok(treeList);
    }

    @DeleteMapping("/{permissionId}")
    public R delSysPermissionById(@PathVariable String permissionId) {
        permissionService.delSysPermissionById(permissionId);
        return R.ok();
    }

}
